//
//  FocusDataMethod.m
//  WRY
//
//  Created by Yang on 9/8/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "FocusDataMethod.h"


@implementation FocusDataMethod
@synthesize delegate; 

static FocusDataMethod *focusDataMethod = nil;

+ (FocusDataMethod *)sharedInstance {
	@synchronized(self) {
		if (focusDataMethod == nil) {
			focusDataMethod = [[FocusDataMethod alloc] init];
			[focusDataMethod initData];
		}
		return focusDataMethod;
	}
	return nil;
}
- (void)initData {
	if ([[NSFileManager defaultManager] fileExistsAtPath:[self dataPath]]) {
		dataArray = [[NSMutableArray alloc] initWithContentsOfFile:[self dataPath]];
	}
	else {
		dataArray = [[NSMutableArray alloc] init];
	}

}
- (void)save {
	[dataArray writeToFile:[self dataPath] atomically:NO];
}
					  
- (NSString *)dataPath {
	return [NSString stringWithFormat:@"%@/Documents/focusData.plist",NSHomeDirectory()];
}
- (NSArray *)data {
	return dataArray;
}
- (BOOL)companyExisting:(NSString *)companyID {
	for (NSDictionary *dict in dataArray) {
		NSString *tempID = [dict objectForKey:@"EnterpriseCode"];
		if ([tempID isEqualToString:companyID]) {
			return YES;
		}
	}
	return NO;
}
- (void)addNewCompany:(NSDictionary *)companyData {

	[dataArray insertObject:companyData atIndex:0];
	[self save];
	[delegate dataHasBeenChanged];
}
- (void)deleteCompany:(NSString *)companyID {
	for (NSDictionary *dict in dataArray) {
		NSString *tempID = [dict objectForKey:@"EnterpriseCode"];
		if ([tempID isEqualToString:companyID]) {
			[dataArray removeObject:dict];
			[self save];
			[delegate dataHasBeenChanged];
			return;
		}
	}
}
- (void)resetData:(NSArray *)array {
	[dataArray setArray:array];
	[self save];
}
@end
